home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / wave.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  19KB  |  551 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Stuff to parse WAVE files.
  5.  
  6. Usage.
  7.  
  8. Reading WAVE files:
  9.       f = wave.open(file, 'r')
  10. where file is either the name of a file or an open file pointer.
  11. The open file pointer must have methods read(), seek(), and close().
  12. When the setpos() and rewind() methods are not used, the seek()
  13. method is not  necessary.
  14.  
  15. This returns an instance of a class with the following public methods:
  16.       getnchannels()  -- returns number of audio channels (1 for
  17.                          mono, 2 for stereo)
  18.       getsampwidth()  -- returns sample width in bytes
  19.       getframerate()  -- returns sampling frequency
  20.       getnframes()    -- returns number of audio frames
  21.       getcomptype()   -- returns compression type ('NONE' for linear samples)
  22.       getcompname()   -- returns human-readable version of
  23.                          compression type ('not compressed' linear samples)
  24.       getparams()     -- returns a tuple consisting of all of the
  25.                          above in the above order
  26.       getmarkers()    -- returns None (for compatibility with the
  27.                          aifc module)
  28.       getmark(id)     -- raises an error since the mark does not
  29.                          exist (for compatibility with the aifc module)
  30.       readframes(n)   -- returns at most n frames of audio
  31.       rewind()        -- rewind to the beginning of the audio stream
  32.       setpos(pos)     -- seek to the specified position
  33.       tell()          -- return the current position
  34.       close()         -- close the instance (make it unusable)
  35. The position returned by tell() and the position given to setpos()
  36. are compatible and have nothing to do with the actual position in the
  37. file.
  38. The close() method is called automatically when the class instance
  39. is destroyed.
  40.  
  41. Writing WAVE files:
  42.       f = wave.open(file, 'w')
  43. where file is either the name of a file or an open file pointer.
  44. The open file pointer must have methods write(), tell(), seek(), and
  45. close().
  46.  
  47. This returns an instance of a class with the following public methods:
  48.       setnchannels(n) -- set the number of channels
  49.       setsampwidth(n) -- set the sample width
  50.       setframerate(n) -- set the frame rate
  51.       setnframes(n)   -- set the number of frames
  52.       setcomptype(type, name)
  53.                       -- set the compression type and the
  54.                          human-readable compression type
  55.       setparams(tuple)
  56.                       -- set all parameters at once
  57.       tell()          -- return current position in output file
  58.       writeframesraw(data)
  59.                       -- write audio frames without pathing up the
  60.                          file header
  61.       writeframes(data)
  62.                       -- write audio frames and patch up the file header
  63.       close()         -- patch up the file header and close the
  64.                          output file
  65. You should set the parameters before the first writeframesraw or
  66. writeframes.  The total number of frames does not need to be set,
  67. but when it is set to the correct value, the header does not have to
  68. be patched up.
  69. It is best to first set all parameters, perhaps possibly the
  70. compression type, and then write audio frames using writeframesraw.
  71. When all frames have been written, either call writeframes('') or
  72. close() to patch up the sizes in the header.
  73. The close() method is called automatically when the class instance
  74. is destroyed.
  75. """
  76. import __builtin__
  77. __all__ = [
  78.     'open',
  79.     'openfp',
  80.     'Error']
  81.  
  82. class Error(Exception):
  83.     pass
  84.  
  85. WAVE_FORMAT_PCM = 1
  86. _array_fmts = (None, 'b', 'h', None, 'l')
  87. import struct
  88. if struct.pack('h', 1) == '\x00\x01':
  89.     big_endian = 1
  90. else:
  91.     big_endian = 0
  92. from chunk import Chunk
  93.  
  94. class Wave_read:
  95.     """Variables used in this class:
  96.  
  97.     These variables are available to the user though appropriate
  98.     methods of this class:
  99.     _file -- the open file with methods read(), close(), and seek()
  100.               set through the __init__() method
  101.     _nchannels -- the number of audio channels
  102.               available through the getnchannels() method
  103.     _nframes -- the number of audio frames
  104.               available through the getnframes() method
  105.     _sampwidth -- the number of bytes per audio sample
  106.               available through the getsampwidth() method
  107.     _framerate -- the sampling frequency
  108.               available through the getframerate() method
  109.     _comptype -- the AIFF-C compression type ('NONE' if AIFF)
  110.               available through the getcomptype() method
  111.     _compname -- the human-readable AIFF-C compression type
  112.               available through the getcomptype() method
  113.     _soundpos -- the position in the audio stream
  114.               available through the tell() method, set through the
  115.               setpos() method
  116.  
  117.     These variables are used internally only:
  118.     _fmt_chunk_read -- 1 iff the FMT chunk has been read
  119.     _data_seek_needed -- 1 iff positioned correctly in audio
  120.               file for readframes()
  121.     _data_chunk -- instantiation of a chunk class for the DATA chunk
  122.     _framesize -- size of one frame in the file
  123.     """
  124.     
  125.     def initfp(self, file):
  126.         self._convert = None
  127.         self._soundpos = 0
  128.         self._file = Chunk(file, bigendian = 0)
  129.         if self._file.getname() != 'RIFF':
  130.             raise Error, 'file does not start with RIFF id'
  131.         self._file.getname() != 'RIFF'
  132.         if self._file.read(4) != 'WAVE':
  133.             raise Error, 'not a WAVE file'
  134.         self._file.read(4) != 'WAVE'
  135.         self._fmt_chunk_read = 0
  136.         self._data_chunk = None
  137.         while None:
  138.             self._data_seek_needed = 1
  139.             
  140.             try:
  141.                 chunk = Chunk(self._file, bigendian = 0)
  142.             except EOFError:
  143.                 break
  144.  
  145.             chunkname = chunk.getname()
  146.             if chunkname == 'fmt ':
  147.                 self._read_fmt_chunk(chunk)
  148.                 self._fmt_chunk_read = 1
  149.             elif chunkname == 'data':
  150.                 if not self._fmt_chunk_read:
  151.                     raise Error, 'data chunk before fmt chunk'
  152.                 self._fmt_chunk_read
  153.                 self._data_chunk = chunk
  154.                 self._nframes = chunk.chunksize // self._framesize
  155.                 self._data_seek_needed = 0
  156.                 break
  157.             
  158.             continue
  159.             if not (self._fmt_chunk_read) or not (self._data_chunk):
  160.                 raise Error, 'fmt chunk and/or data chunk missing'
  161.             not (self._data_chunk)
  162.             return None
  163.  
  164.     
  165.     def __init__(self, f):
  166.         self._i_opened_the_file = None
  167.         if isinstance(f, basestring):
  168.             f = __builtin__.open(f, 'rb')
  169.             self._i_opened_the_file = f
  170.         
  171.         
  172.         try:
  173.             self.initfp(f)
  174.         except:
  175.             if self._i_opened_the_file:
  176.                 f.close()
  177.             
  178.             raise 
  179.  
  180.  
  181.     
  182.     def __del__(self):
  183.         self.close()
  184.  
  185.     
  186.     def getfp(self):
  187.         return self._file
  188.  
  189.     
  190.     def rewind(self):
  191.         self._data_seek_needed = 1
  192.         self._soundpos = 0
  193.  
  194.     
  195.     def close(self):
  196.         if self._i_opened_the_file:
  197.             self._i_opened_the_file.close()
  198.             self._i_opened_the_file = None
  199.         
  200.         self._file = None
  201.  
  202.     
  203.     def tell(self):
  204.         return self._soundpos
  205.  
  206.     
  207.     def getnchannels(self):
  208.         return self._nchannels
  209.  
  210.     
  211.     def getnframes(self):
  212.         return self._nframes
  213.  
  214.     
  215.     def getsampwidth(self):
  216.         return self._sampwidth
  217.  
  218.     
  219.     def getframerate(self):
  220.         return self._framerate
  221.  
  222.     
  223.     def getcomptype(self):
  224.         return self._comptype
  225.  
  226.     
  227.     def getcompname(self):
  228.         return self._compname
  229.  
  230.     
  231.     def getparams(self):
  232.         return (self.getnchannels(), self.getsampwidth(), self.getframerate(), self.getnframes(), self.getcomptype(), self.getcompname())
  233.  
  234.     
  235.     def getmarkers(self):
  236.         pass
  237.  
  238.     
  239.     def getmark(self, id):
  240.         raise Error, 'no marks'
  241.  
  242.     
  243.     def setpos(self, pos):
  244.         if pos < 0 or pos > self._nframes:
  245.             raise Error, 'position not in range'
  246.         pos > self._nframes
  247.         self._soundpos = pos
  248.         self._data_seek_needed = 1
  249.  
  250.     
  251.     def readframes(self, nframes):
  252.         if self._data_seek_needed:
  253.             self._data_chunk.seek(0, 0)
  254.             pos = self._soundpos * self._framesize
  255.             if pos:
  256.                 self._data_chunk.seek(pos, 0)
  257.             
  258.             self._data_seek_needed = 0
  259.         
  260.         if nframes == 0:
  261.             return ''
  262.         if self._convert and data:
  263.             data = self._convert(data)
  264.         
  265.         self._soundpos = self._soundpos + len(data) // self._nchannels * self._sampwidth
  266.         return data
  267.  
  268.     
  269.     def _read_fmt_chunk(self, chunk):
  270.         (wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign) = struct.unpack('<hhllh', chunk.read(14))
  271.         if wFormatTag == WAVE_FORMAT_PCM:
  272.             sampwidth = struct.unpack('<h', chunk.read(2))[0]
  273.             self._sampwidth = (sampwidth + 7) // 8
  274.         else:
  275.             raise Error, 'unknown format: %r' % (wFormatTag,)
  276.         self._framesize = (wFormatTag == WAVE_FORMAT_PCM)._nchannels * self._sampwidth
  277.         self._comptype = 'NONE'
  278.         self._compname = 'not compressed'
  279.  
  280.  
  281.  
  282. class Wave_write:
  283.     """Variables used in this class:
  284.  
  285.     These variables are user settable through appropriate methods
  286.     of this class:
  287.     _file -- the open file with methods write(), close(), tell(), seek()
  288.               set through the __init__() method
  289.     _comptype -- the AIFF-C compression type ('NONE' in AIFF)
  290.               set through the setcomptype() or setparams() method
  291.     _compname -- the human-readable AIFF-C compression type
  292.               set through the setcomptype() or setparams() method
  293.     _nchannels -- the number of audio channels
  294.               set through the setnchannels() or setparams() method
  295.     _sampwidth -- the number of bytes per audio sample
  296.               set through the setsampwidth() or setparams() method
  297.     _framerate -- the sampling frequency
  298.               set through the setframerate() or setparams() method
  299.     _nframes -- the number of audio frames written to the header
  300.               set through the setnframes() or setparams() method
  301.  
  302.     These variables are used internally only:
  303.     _datalength -- the size of the audio samples written to the header
  304.     _nframeswritten -- the number of frames actually written
  305.     _datawritten -- the size of the audio samples actually written
  306.     """
  307.     
  308.     def __init__(self, f):
  309.         self._i_opened_the_file = None
  310.         if isinstance(f, basestring):
  311.             f = __builtin__.open(f, 'wb')
  312.             self._i_opened_the_file = f
  313.         
  314.         
  315.         try:
  316.             self.initfp(f)
  317.         except:
  318.             if self._i_opened_the_file:
  319.                 f.close()
  320.             
  321.             raise 
  322.  
  323.  
  324.     
  325.     def initfp(self, file):
  326.         self._file = file
  327.         self._convert = None
  328.         self._nchannels = 0
  329.         self._sampwidth = 0
  330.         self._framerate = 0
  331.         self._nframes = 0
  332.         self._nframeswritten = 0
  333.         self._datawritten = 0
  334.         self._datalength = 0
  335.  
  336.     
  337.     def __del__(self):
  338.         self.close()
  339.  
  340.     
  341.     def setnchannels(self, nchannels):
  342.         if self._datawritten:
  343.             raise Error, 'cannot change parameters after starting to write'
  344.         self._datawritten
  345.         if nchannels < 1:
  346.             raise Error, 'bad # of channels'
  347.         nchannels < 1
  348.         self._nchannels = nchannels
  349.  
  350.     
  351.     def getnchannels(self):
  352.         if not self._nchannels:
  353.             raise Error, 'number of channels not set'
  354.         self._nchannels
  355.         return self._nchannels
  356.  
  357.     
  358.     def setsampwidth(self, sampwidth):
  359.         if self._datawritten:
  360.             raise Error, 'cannot change parameters after starting to write'
  361.         self._datawritten
  362.         if sampwidth < 1 or sampwidth > 4:
  363.             raise Error, 'bad sample width'
  364.         sampwidth > 4
  365.         self._sampwidth = sampwidth
  366.  
  367.     
  368.     def getsampwidth(self):
  369.         if not self._sampwidth:
  370.             raise Error, 'sample width not set'
  371.         self._sampwidth
  372.         return self._sampwidth
  373.  
  374.     
  375.     def setframerate(self, framerate):
  376.         if self._datawritten:
  377.             raise Error, 'cannot change parameters after starting to write'
  378.         self._datawritten
  379.         if framerate <= 0:
  380.             raise Error, 'bad frame rate'
  381.         framerate <= 0
  382.         self._framerate = framerate
  383.  
  384.     
  385.     def getframerate(self):
  386.         if not self._framerate:
  387.             raise Error, 'frame rate not set'
  388.         self._framerate
  389.         return self._framerate
  390.  
  391.     
  392.     def setnframes(self, nframes):
  393.         if self._datawritten:
  394.             raise Error, 'cannot change parameters after starting to write'
  395.         self._datawritten
  396.         self._nframes = nframes
  397.  
  398.     
  399.     def getnframes(self):
  400.         return self._nframeswritten
  401.  
  402.     
  403.     def setcomptype(self, comptype, compname):
  404.         if self._datawritten:
  405.             raise Error, 'cannot change parameters after starting to write'
  406.         self._datawritten
  407.         if comptype not in ('NONE',):
  408.             raise Error, 'unsupported compression type'
  409.         comptype not in ('NONE',)
  410.         self._comptype = comptype
  411.         self._compname = compname
  412.  
  413.     
  414.     def getcomptype(self):
  415.         return self._comptype
  416.  
  417.     
  418.     def getcompname(self):
  419.         return self._compname
  420.  
  421.     
  422.     def setparams(self, .1):
  423.         (nchannels, sampwidth, framerate, nframes, comptype, compname) = .1
  424.         if self._datawritten:
  425.             raise Error, 'cannot change parameters after starting to write'
  426.         self._datawritten
  427.         self.setnchannels(nchannels)
  428.         self.setsampwidth(sampwidth)
  429.         self.setframerate(framerate)
  430.         self.setnframes(nframes)
  431.         self.setcomptype(comptype, compname)
  432.  
  433.     
  434.     def getparams(self):
  435.         if not (self._nchannels) and not (self._sampwidth) or not (self._framerate):
  436.             raise Error, 'not all parameters set'
  437.         not (self._framerate)
  438.         return (self._nchannels, self._sampwidth, self._framerate, self._nframes, self._comptype, self._compname)
  439.  
  440.     
  441.     def setmark(self, id, pos, name):
  442.         raise Error, 'setmark() not supported'
  443.  
  444.     
  445.     def getmark(self, id):
  446.         raise Error, 'no marks'
  447.  
  448.     
  449.     def getmarkers(self):
  450.         pass
  451.  
  452.     
  453.     def tell(self):
  454.         return self._nframeswritten
  455.  
  456.     
  457.     def writeframesraw(self, data):
  458.         self._ensure_header_written(len(data))
  459.         nframes = len(data) // self._sampwidth * self._nchannels
  460.         if self._convert:
  461.             data = self._convert(data)
  462.         
  463.         if self._sampwidth > 1 and big_endian:
  464.             import array
  465.             data = array.array(_array_fmts[self._sampwidth], data)
  466.             data.byteswap()
  467.             data.tofile(self._file)
  468.             self._datawritten = self._datawritten + len(data) * self._sampwidth
  469.         else:
  470.             self._file.write(data)
  471.             self._datawritten = self._datawritten + len(data)
  472.         self._nframeswritten = self._nframeswritten + nframes
  473.  
  474.     
  475.     def writeframes(self, data):
  476.         self.writeframesraw(data)
  477.         if self._datalength != self._datawritten:
  478.             self._patchheader()
  479.         
  480.  
  481.     
  482.     def close(self):
  483.         if self._file:
  484.             self._ensure_header_written(0)
  485.             if self._datalength != self._datawritten:
  486.                 self._patchheader()
  487.             
  488.             self._file.flush()
  489.             self._file = None
  490.         
  491.         if self._i_opened_the_file:
  492.             self._i_opened_the_file.close()
  493.             self._i_opened_the_file = None
  494.         
  495.  
  496.     
  497.     def _ensure_header_written(self, datasize):
  498.         if not self._datawritten:
  499.             if not self._nchannels:
  500.                 raise Error, '# channels not specified'
  501.             self._nchannels
  502.             if not self._sampwidth:
  503.                 raise Error, 'sample width not specified'
  504.             self._sampwidth
  505.             if not self._framerate:
  506.                 raise Error, 'sampling rate not specified'
  507.             self._framerate
  508.             self._write_header(datasize)
  509.         
  510.  
  511.     
  512.     def _write_header(self, initlength):
  513.         self._file.write('RIFF')
  514.         if not self._nframes:
  515.             self._nframes = initlength / self._nchannels * self._sampwidth
  516.         
  517.         self._datalength = self._nframes * self._nchannels * self._sampwidth
  518.         self._form_length_pos = self._file.tell()
  519.         self._file.write(struct.pack('<l4s4slhhllhh4s', 36 + self._datalength, 'WAVE', 'fmt ', 16, WAVE_FORMAT_PCM, self._nchannels, self._framerate, self._nchannels * self._framerate * self._sampwidth, self._nchannels * self._sampwidth, self._sampwidth * 8, 'data'))
  520.         self._data_length_pos = self._file.tell()
  521.         self._file.write(struct.pack('<l', self._datalength))
  522.  
  523.     
  524.     def _patchheader(self):
  525.         if self._datawritten == self._datalength:
  526.             return None
  527.         curpos = self._file.tell()
  528.         self._file.seek(self._form_length_pos, 0)
  529.         self._file.write(struct.pack('<l', 36 + self._datawritten))
  530.         self._file.seek(self._data_length_pos, 0)
  531.         self._file.write(struct.pack('<l', self._datawritten))
  532.         self._file.seek(curpos, 0)
  533.         self._datalength = self._datawritten
  534.  
  535.  
  536.  
  537. def open(f, mode = None):
  538.     if mode is None:
  539.         if hasattr(f, 'mode'):
  540.             mode = f.mode
  541.         else:
  542.             mode = 'rb'
  543.     
  544.     if mode in ('r', 'rb'):
  545.         return Wave_read(f)
  546.     if mode in ('w', 'wb'):
  547.         return Wave_write(f)
  548.     raise Error, "mode must be 'r', 'rb', 'w', or 'wb'"
  549.  
  550. openfp = open
  551.